﻿@namespace BootstrapBlazor.Components
@inherits ComponentBase

@{
    #region 用来提交筛选的本地函数
    Task OnSubmit<Obj>(Obj value)
    => RenderPropertyInfo.Submit();
    #endregion
    var conditionGroup = RenderPropertyInfo.RenderConditionGroup;
    var filterObjectType = conditionGroup.FilterObjectType;
    var query = conditionGroup.RenderFilterQuery;
    var sort = conditionGroup.RenderFilterSort;
    var state = RenderPropertyInfo.SearchViewerState;
    var describe = conditionGroup.Describe;
    #region 当按下回车键时触发的委托
    Func<T, Task> OnEnter<T>()
    => async (_) => await RenderPropertyInfo.Submit();
    #endregion
    #region 绑定查询条件
    IBind<T> BindQuery<T>()
    => state.Bind<T>(query);
    #endregion
    #region 绑定查询条件
    IBindProperty<SortStatus>? BindSort()
    => sort is null ? null : state.Bind(sort);
    #endregion
    #region 日期确定时的方法
    Func<DateTimeRangeValue, Task> OnDateConfirm(BindRange<DateTimeOffset?> dateRange)
    => async value =>
    {
        dateRange.Start = value.Start;
        dateRange.End = value.End;
        await RenderPropertyInfo.Submit();
    };
    #endregion
    #region 日期清空时的方法
    Func<DateTimeRangeValue, Task> OnDateClear(BindRange<DateTimeOffset?> dateRange)
    => async value =>
    {
        dateRange.Start = null;
        dateRange.End = null;
        await RenderPropertyInfo.Submit();
    };
    #endregion
    var @enumItem = query.FilterTarget.EnumItem;
}
<div class="bootstrapSearchPanelPropertyContainer">
    @switch ((filterObjectType, enumItem))
    {
        case (FilterObjectType.Enum, _) or (_, { Count: > 0 }):
            var bindQueryEnum = BindQuery<string>().ToBindProperty();
            var bindSortEnum = BindSort();
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel DisplayText="@describe" />
                @if (enumItem.Count >= 10)
                {
                    <BootstrapInput Value="@bindQueryEnum.Value" Readonly="true" />
                    <Button Text="选择" OnClick="@(()=>IsShow=true)" />
                }
                else
                {
                    <Select TValue="string" @bind-Value=@(bindQueryEnum.Value) OnValueChanged="OnSubmit">
                        <Options>
                            <SelectOption Text="未选择" Value="@null" />
                            @foreach (var item in enumItem)
                            {
                                <SelectOption Text="@item.Describe" Value="@item.Value" />
                            }
                        </Options>
                    </Select>
                }
                @if (bindSortEnum is { })
                {
                    <Select @bind-Value=@(bindSortEnum.Value) OnValueChanged="OnSubmit" />
                }
            </BootstrapInputGroup>
            @if (IsShow)
            {
                <ModelDialog Context="dialogID">
                    <dialog id="@dialogID">
                        <Selector Candidates="enumItem.ToHashSet()" Submit="SubmitEnumItem(bindQueryEnum)"
                                  InitializationSelect="@(x=>x.Value==bindQueryEnum.Value)" @ref=Selector>
                            <RenderComponent Context="renderSelectorInfo">
                                <Card>
                                    <HeaderTemplate>
                                        <Center>
                                            <div>@($"选择{describe}")</div>
                                        </Center>
                                    </HeaderTemplate>
                                    <BodyTemplate>
                                        <div class="bootstrapSearchPanelSelect">
                                            @foreach (var renderSelectorElementInfo in renderSelectorInfo.CandidatesInfo)
                                            {
                                                var selectorElementDescribe = renderSelectorElementInfo.Element.Describe;
                                                <div @onclick=@renderSelectorElementInfo.ChangeSelect>
                                                    <div class="@(renderSelectorElementInfo.IsSelect?"selectElement":"") selectorElementContainer">
                                                        @selectorElementDescribe
                                                    </div>
                                                </div>
                                            }
                                        </div>
                                    </BodyTemplate>
                                    <FooterTemplate>
                                        <div class="compactCentered">
                                            <Button Text="清除条件" OnClick="ClearEnumItem(bindQueryEnum)" />
                                            <Button Text="提交搜索" OnClick="renderSelectorInfo.Submit" IsDisabled="@(renderSelectorInfo.SelectElementInfo.IsInvalid)" />
                                            <Button Text="返回页面" OnClick="@(()=>IsShow=false)" />
                                        </div>
                                    </FooterTemplate>
                                </Card>
                            </RenderComponent>
                        </Selector>
                    </dialog>
                </ModelDialog>
            }
            break;
        case (FilterObjectType.Text, _):
            var bindQueryText = BindQuery<string>().ToBindProperty();
            var bindSortText = BindSort();
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel DisplayText="@describe" />
                <BootstrapInput @bind-Value=@(bindQueryText.Value) OnEnterAsync="@(OnEnter<string?>())" IsSelectAllTextOnFocus=true />
                @if (bindSortText is { })
                {
                    <Select @bind-Value=@(bindSortText.Value) OnValueChanged="OnSubmit" />
                }
            </BootstrapInputGroup>
            break;
        case (FilterObjectType.Num, _):
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel DisplayText="@describe" />
                @{
                    var bindQueryNum = BindQuery<double?>();
                    var bindSortNum = BindSort();
                }
                @switch (bindQueryNum)
                {
                    case IBindProperty<double?> bindQueryNumSingle:
                        <BootstrapInputNumber @bind-Value=@(bindQueryNumSingle.Value) OnEnterAsync="@(OnEnter<double?>())" IsSelectAllTextOnFocus=true />
                        break;
                    case IBindRange<double?> { Range: { } bindQueryNumRange }:
                        <BootstrapInputNumber @bind-Value=@(bindQueryNumRange.Start) OnEnterAsync="@(OnEnter<double?>())" IsSelectAllTextOnFocus=true />
                        <BootstrapInputGroupLabel DisplayText="至" />
                        <BootstrapInputNumber @bind-Value=@(bindQueryNumRange.End) OnEnterAsync="@(OnEnter<double?>())" IsSelectAllTextOnFocus=true />
                        break;
                }
                @if (bindSortNum is { })
                {
                    <Select @bind-Value=@(bindSortNum.Value) OnValueChanged="OnSubmit" />
                }
            </BootstrapInputGroup>
            break;
        case (FilterObjectType.Date, _):
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel DisplayText="@describe" />
                @{
                    var bindQueryDate = BindQuery<DateTimeOffset?>();
                    var bindSortDate = BindSort();
                }
                @switch (bindQueryDate)
                {
                    case IBindProperty<DateTimeOffset?> bindQueryDateSingle:
                        <DateTimePicker @bind-Value=@(bindQueryDateSingle.Value) OnValueChanged="OnSubmit" />
                        break;
                    case IBindRange<DateTimeOffset?> { Range: { } bindQueryDateRange }:
                        var dateTimeRangeValue = new DateTimeRangeValue()
                        {
                            NullStart = bindQueryDateRange.Start?.DateTime,
                            NullEnd = bindQueryDateRange.End?.DateTime
                        };
                        <DateTimeRange ShowToday="true" Value="dateTimeRangeValue" OnConfirm="OnDateConfirm(bindQueryDateRange)" OnClearValue="OnDateClear(bindQueryDateRange)" />
                        break;
                }
                @if (bindSortDate is { })
                {
                    <Select @bind-Value=@(bindSortDate.Value) OnValueChanged="OnSubmit" />
                }
            </BootstrapInputGroup>
            break;
        case (FilterObjectType.Bool, _):
            var bindQueryBool = BindQuery<bool>().ToBindProperty();
            <BootstrapInputGroup>
                <BootstrapInputGroupLabel DisplayText="@describe" />
                <Switch @bind-Value=@(bindQueryBool.Value)
                        OnValueChanged="OnSubmit" OnText="开启" OffText="关闭" />
            </BootstrapInputGroup>
            break;
        default:
            throw new NotSupportedException($"暂不支持渲染{filterObjectType}类型的属性");
    }
</div>
